package com.example.oasystem.service.impl;

import com.example.oasystem.dao.systemcontrol.SystemcontrolDao;
import com.example.oasystem.entity.daymanage.AoaScheduleListOfSun;
import com.example.oasystem.entity.notepaper.AoaNotepaperOfSun1;
import com.example.oasystem.entity.notice.AoaNoticeListOfSun;
import com.example.oasystem.entity.notice.NoticeDTO;
import com.example.oasystem.entity.plan.AoaPlanListOfSun;
import com.example.oasystem.entity.process.AoaNotepaperOfSun;
import com.example.oasystem.entity.process.AoaProcess;
import com.example.oasystem.entity.status.AoaStatusList;
import com.example.oasystem.entity.type.AoaTypeList;
import com.example.oasystem.service.SystemcontrolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class SystemcontrolServiceImpl implements SystemcontrolService {
   
    @Autowired
    private SystemcontrolDao systemcontrolDao;
    
    @Override
    public int selectFile(Long userId) {
        return systemcontrolDao.selectFile(userId);
    }

    @Override
    public int selectDirector(Long userId) {
        return systemcontrolDao.selectDirector(userId);
    }

    @Override
    public List<AoaNoticeListOfSun> selectNotice(Long userId) {
        return systemcontrolDao.selectNotice(userId);
    }

    @Override
    public List<AoaProcess> selectProcess(Long userId) {
        return systemcontrolDao.selectProcess(userId);
    }

    @Override
    public List<AoaPlanListOfSun> selectPlan(Long userId) {
        return systemcontrolDao.selectPlan(userId);
    }

    @Override
    public List<AoaTypeList> selectTypeList() {
        return systemcontrolDao.selectTypeList();
    }

    @Override
    public List<AoaStatusList> selectStatusList() {
        return systemcontrolDao.selectStatusList();
    }

    @Override
    public List<AoaNotepaperOfSun1> selectNotepaperList(Long userId,String concent) {
        return systemcontrolDao.selectNotepaperList(userId,concent);
    }

    @Override
    public AoaPlanListOfSun selectOnePlan(long planId) {
        return systemcontrolDao.selectOnePlan(planId);
    }

    @Override
    public List<AoaScheduleListOfSun> findstart(long userid) {
        return systemcontrolDao.findstart(userid);
    }

    @Override
    public void addnotepaper(AoaNotepaperOfSun1 sun) {
        systemcontrolDao.addnotepaper(sun);
    }


    public List<String> getUserByName(){
        List<String> name = systemcontrolDao.getUserByName();
        Map<String,Integer> a= new HashMap<>();
        for (String s : name) {
            int  count=systemcontrolDao.getUserTask(s);
            a.put(s,count);
        }
        List<String> newList=new ArrayList<>();
        newList=sortMap(a);
        return newList;
    }

    public List<String> sortMap(Map<String, Integer> map) {

        //获取entrySet
        Set<Map.Entry<String, Integer>> mapEntries = map.entrySet();

        //使用链表来对集合进行排序，使用LinkedList，利于插入元素
        List<Map.Entry<String, Integer>> result = new LinkedList<>(mapEntries);
        //自定义比较器来比较链表中的元素
        Collections.sort(result, new Comparator<Map.Entry<String, Integer>>() {
            //基于entry的值（Entry.getValue()），来排序链表
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o2.getValue().compareTo(o1.getValue());
            }
        });

        //将排好序的存入到LinkedHashMap(可保持顺序)中，需要存储键和值信息对到新的映射中。
        Integer sort = 1;
        Map<String, Integer> linkMap = new LinkedHashMap<>();
        List<String> newList=new ArrayList<>();
        for (Map.Entry<String, Integer> newEntry : result) {
            // 取出排名前5的值
            if (sort <= 5) {
                linkMap.put(newEntry.getKey(), newEntry.getValue());
                newList.add(" "+newEntry.getKey()+" :"+ newEntry.getValue());
                ++sort;
            }
        }
        return newList;
    }


}
